package com.agu.mgt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.agu.common.annotation.SysLog;
import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;
import com.agu.entity.sys.SysMenuEntity;
import com.agu.entity.sys.SysRoleEntity;
import com.agu.mgt.sys.dto.JsTreeNode;
import com.agu.mgt.sys.service.SysMenuService;
import com.agu.mgt.sys.service.SysRoleMenuService;
import com.agu.mgt.sys.service.SysRoleService;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.CompareUtils;
import com.agu.utils.Query;
import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 
 * @author 
 * @email 
 * @date 2018-10-16 14:43:49
 */
 @SuppressWarnings("rawtypes")
@RestController
@Api("")
@RequestMapping("sysrole")
public class SysRoleController extends AbstractController{
	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysMenuService sysMenuService;
	
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("sysrole:list")
	public Result list(@RequestBody Map params){

		// 查询列表数据
		Query query = new Query(params);
		Page<SysRoleEntity> page = sysRoleService.queryPageList(query);
		return ResultGenerator.successPage(page);
	}
	
	/**
	 * 授权菜单
	 * @return
	 */
	@RequestMapping("/menus")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("sysrole:list")
	public Result menus(@RequestParam(value="plat", defaultValue="1") int plat){

		// 查询列表数据
		List<SysMenuEntity> list = sysMenuService.getAllMenuList(plat);
		
		List<JsTreeNode> nodelist = new ArrayList<JsTreeNode>();
		
		for(SysMenuEntity menu:list)
		{
			JsTreeNode node = new JsTreeNode(menu);
			nodelist.add(node);
		}
			
		List<JsTreeNode> menuList = JsTreeNode.buildTree(nodelist);
		
		Map map = new HashMap();
		map.put("menuList", menuList);
		
		return ResultGenerator.successData(map);
	}
	/**
	 * 信息
	 */
	@GetMapping("/info/{roleId}")
	@ApiOperation(value = "信息", notes = "说明")
	@RequiresPermissions("sysrole:info")
	public Result info(@PathVariable("roleId") Long roleId){
		SysRoleEntity sysRole = sysRoleService.queryObject(roleId);
		
		
		// 查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		sysRole.setMenuIdList(menuIdList);
		
		return ResultGenerator.successData(sysRole);
	}
	
	/**
	 * 保存
	 */
	@SysLog("新增角色")
	@PostMapping("/save")
	@ApiOperation(value = "保存", notes = "说明")
	@RequiresPermissions("sysrole:save")
	public Result save(@RequestBody SysRoleEntity sysRole){
		
		sysRoleService.save(sysRole);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("roleId","ID");
		     cmap.put("roleName","角色名称");
		     cmap.put("remark","备注");

		String remark = CompareUtils.getObjectRemark(sysRole, cmap);
		return ResultGenerator.success("保存成功").setRemark(remark);
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改角色")
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("sysrole:update")
	public Result update(@RequestBody SysRoleEntity sysRole){
		
		SysRoleEntity sysRole1 = sysRoleService.queryObject(sysRole.getRoleId());
		
		sysRoleService.update(sysRole);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		
		     cmap.put("roleId","ID");
		     cmap.put("roleName","角色名称");
		     cmap.put("remark","备注");

	
		String remark = CompareUtils.getTwoObjectRemark(sysRole1, sysRole, cmap);
	
		return ResultGenerator.success("修改成功").setRemark(remark);
	}
	
	
	/**
	 * 删除
	 */
	@SysLog("删除角色")
	@GetMapping("/delete/{roleId}")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("sysrole:delete")
	public Result delete(@PathVariable("roleId") Long roleId){
	
	    	if (roleId == null) {
			return ResultGenerator.fail("缺少参数");
		}
	
		SysRoleEntity sysRole = sysRoleService.queryObject(roleId);
	
		sysRoleService.delete(roleId);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("roleId","ID");
		     cmap.put("roleName","角色名称");
		     cmap.put("remark","备注");

		String remark = CompareUtils.getObjectRemark(sysRole, cmap);
		return ResultGenerator.success("删除成功").setRemark(remark);
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除角色")
	@PostMapping("/deletes")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("sysrole:delete")
	public Result deletes(@RequestBody Long[] roleIds){
		sysRoleService.deleteBatch(roleIds);
		
		return ResultGenerator.success("删除成功");
	}
	
	@RequestMapping("/download")
	@ApiOperation(value = "下载", notes = "说明")
	@RequiresPermissions("sysrole:list")
	public void download(HttpServletRequest request, HttpServletResponse response) {

		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		
		String query = orgRequest.getParameter("query");

		HashMap querymap = JSON.parseObject(query, HashMap.class);

		List<SysRoleEntity> list = sysRoleService.queryList(querymap);

		if (list != null && list.size() > 0) {
			Map map = new HashMap();


			List<Column> clist = new ArrayList();
			Column cloumn = null;

            cloumn = new Column("ID", "roleId", 1);
			clist.add(cloumn);
            cloumn = new Column("角色名称", "roleName", 1);
			clist.add(cloumn);
            cloumn = new Column("备注", "remark", 1);
			clist.add(cloumn);
            cloumn = new Column("创建者ID", "createUserId", 1);
			clist.add(cloumn);
            cloumn = new Column("创建时间", "createTime", 1);
			clist.add(cloumn);
			try {
				ExcelExport export = exportList(list, clist, map);
				if (export != null) {
					this.downExcel(export, response);
					//this.downzip(export, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
